package cn.zhangmh.easymvp.books.ui;

import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.panxw.android.imageindicator.AutoPlayManager;
import com.panxw.android.imageindicator.ImageIndicatorView;

import cn.zhangmh.easymvp.R;
import cn.zhangmh.easymvp.utils.StringUtils;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link SearchFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link SearchFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SearchFragment extends Fragment implements View.OnClickListener {

    private AutoCompleteTextView input;
    private ImageButton search_btn;
    private ImageIndicatorView imageIndicatorView;
    private  DialogFragment dialog = new DialogFragment();

    // TODO: Rename and change types of parameters
    private String keyWords;


    private OnFragmentInteractionListener mListener;

    public SearchFragment() {
        // Required empty public constructor
    }


    public static SearchFragment newInstance() {
        SearchFragment fragment = new SearchFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, container, false);
        imageIndicatorView =  (ImageIndicatorView)view.findViewById(R.id.indicate_view);
        final  Integer[] array = new  Integer[]{R.drawable.b01,R.drawable.b02,R.drawable.b03};
        imageIndicatorView.setupLayoutByDrawable(array);
        imageIndicatorView.setIndicateStyle(ImageIndicatorView.INDICATE_ARROW_ROUND_STYLE);
        AutoPlayManager autoBrocastManager =  new AutoPlayManager(imageIndicatorView);
        autoBrocastManager.setBroadcastEnable(true);
        autoBrocastManager.setBroadCastTimes(5);//loop times
        autoBrocastManager.setBroadcastTimeIntevel(3 * 1000, 3 * 1000);//set first play time and interval
        autoBrocastManager.loop();
        imageIndicatorView.show();
        input = (AutoCompleteTextView)view.findViewById(R.id.autoCompleteTextView);
        search_btn = (ImageButton) view.findViewById(R.id.search_btn);
        search_btn.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
        InputMethodManager imm =    ((InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE));
         if (imm.isActive())
         {
             imm.hideSoftInputFromWindow(v.getApplicationWindowToken(),0);
         }
            String words = input.getText().toString();
            if (!StringUtils.isBlank(words)) {
                mListener.onFragmentInteraction(words);
            }else{
                Toast.makeText(getActivity().getApplication(),"请输入有效的查询词！",Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(String  keywords);
    }


    /**
     * 由activity显式调用
     * @param mListener
     */
    public  void  setOnFragmentInteractionListener(OnFragmentInteractionListener mListener  ){
        this.mListener = mListener;
    }

}
